package com.xdtech.trans.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.utils.DateUtil;
import com.xdtech.common.utils.DateUtil.DateStyle;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.trans.dao.TransOrderTrackDao;
import com.xdtech.trans.model.TransOrder;
import com.xdtech.trans.model.TransOrderScan;
import com.xdtech.trans.model.TransOrderTrack;
import com.xdtech.trans.service.TransOrderService;
import com.xdtech.trans.service.TransOrderTrackService;
import com.xdtech.trans.util.TransConstants;
import com.xdtech.trans.vo.TransOrderTrackItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-06-08 23:21:29
 * @since 1.0
 * @see
 */
@Service
public class TransOrderTrackServiceImpl implements TransOrderTrackService {
	private Log log = LogFactory.getLog(TransOrderTrackServiceImpl.class);
	@Autowired
	private TransOrderTrackDao transOrderTrackDao;
	@Autowired
	private BaseDao baseDao;
	
	@Autowired
	private TransOrderService transOrderService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param entity
	 */
	public void save(TransOrderTrack entity) {
		transOrderTrackDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<TransOrderTrack> entities) {
		transOrderTrackDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param entity
	 */
	public void delete(TransOrderTrack entity) {
		transOrderTrackDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		transOrderTrackDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param id
	 * @return
	 */
	public TransOrderTrack get(Long id) {
		return transOrderTrackDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @return
	 */
	public List<TransOrderTrack> getAll() {
		return transOrderTrackDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> transOrderTracks = null;
		long rows = 0;
		if (pg!=null) {
			Page<TransOrderTrack> page = transOrderTrackDao.findPage(pg,"from TransOrderTrack order by createTime desc", values);
			transOrderTracks = BeanUtils.copyListProperties(
					TransOrderTrackItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<TransOrderTrack> transOrderTrackList = transOrderTrackDao.find("from TransOrderTrack order by id desc", values);
			transOrderTracks = BeanUtils.copyListProperties(
					TransOrderTrackItem.class, transOrderTrackList);
			rows = transOrderTracks.size();
		}
		results.put("total", rows);
		results.put("rows", transOrderTracks);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateTransOrderTrack(TransOrderTrackItem item) {
		TransOrderTrack transOrderTrack = null;
		if (item.getId()==null) {
			transOrderTrack = new TransOrderTrack();
		}else {
			transOrderTrack = transOrderTrackDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(transOrderTrack, item);
		transOrderTrackDao.save(transOrderTrack);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param transOrderTrackId
	 * @return
	 */
	public TransOrderTrackItem loadTransOrderTrackItem(Long transOrderTrackId) {
		TransOrderTrack transOrderTrack = transOrderTrackDao.get(transOrderTrackId);
		return TransOrderTrackItem.createItem(transOrderTrack);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteTransOrderTrackInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteTransOrderTrackInfo(List<Long> transOrderTrackIds) {
		for (Long id : transOrderTrackIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-08 23:21:29
	 * @modified by
	 * @return
	 */
	@Override
	public List<TransOrderTrackItem> loadItems() {
		List<TransOrderTrack> transOrderTracks = getAll();
		return TransOrderTrackItem.createItems(transOrderTracks);
	}
	/* (非 Javadoc)
	 * startTrack
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param transOrderScan
	 * @see com.xdtech.trans.service.TransOrderTrackService#startTrack(com.xdtech.trans.model.TransOrderScan)
	 */
	@Override
	public void startTrack(TransOrderScan transOrderScan)
	{
		TransOrderTrack transOrderTrack = new TransOrderTrack();
		transOrderTrack.setOrderNo(transOrderScan.getOrderNo());
		transOrderTrack.setTrackTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
		transOrderTrack.setTrackType("1");
		transOrderTrack.setTrackInfo(String.format("营业网点【%s】已收货，进入公司分拣", transOrderScan.getStartWebsite()));
		save(transOrderTrack);
	}
	/* (非 Javadoc)
	 * sendTrack
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param transOrderScan
	 * @see com.xdtech.trans.service.TransOrderTrackService#sendTrack(com.xdtech.trans.model.TransOrderScan)
	 */
	@Override
	public void sendTrack(TransOrderScan transOrderScan)
	{
		TransOrderTrack transOrderTrack = new TransOrderTrack();
		transOrderTrack.setOrderNo(transOrderScan.getOrderNo());
		transOrderTrack.setTrackTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
		transOrderTrack.setTrackType("2");
		transOrderTrack.setTrackInfo(String.format("营业网点【%s】已发货，下一站【%s】", transOrderScan.getCurrentWebsite(),transOrderScan.getNextWebsite()));
		save(transOrderTrack);
	}
	/* (非 Javadoc)
	 * arriveTrack
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param transOrderScan
	 * @see com.xdtech.trans.service.TransOrderTrackService#arriveTrack(com.xdtech.trans.model.TransOrderScan)
	 */
	@Override
	public void arriveTrack(TransOrderScan transOrderScan)
	{
		TransOrderTrack transOrderTrack = new TransOrderTrack();
		transOrderTrack.setOrderNo(transOrderScan.getOrderNo());
		transOrderTrack.setTrackTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
		transOrderTrack.setTrackType("3");
		transOrderTrack.setTrackInfo(String.format("到达网点【%s】", transOrderScan.getCurrentWebsite()));
		save(transOrderTrack);
	}
	/* (非 Javadoc)
	 * deliveryTrack
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param transOrderScan
	 * @see com.xdtech.trans.service.TransOrderTrackService#deliveryTrack(com.xdtech.trans.model.TransOrderScan)
	 */
	@Override
	public void deliveryTrack(TransOrderScan transOrderScan)
	{
		TransOrderTrack transOrderTrack = new TransOrderTrack();
		transOrderTrack.setOrderNo(transOrderScan.getOrderNo());
		transOrderTrack.setTrackTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
		transOrderTrack.setTrackType("4");
		transOrderTrack.setTrackInfo(String.format("网点【%s】已派送，派送员：%s，派送员电话：%s", transOrderScan.getCurrentWebsite(),transOrderScan.getDeliveryName(),transOrderScan.getDeliveryPhone()));
		save(transOrderTrack);
	}
	/* (非 Javadoc)
	 * signTrack
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param transOrderScan
	 * @see com.xdtech.trans.service.TransOrderTrackService#signTrack(com.xdtech.trans.model.TransOrderScan)
	 */
	@Override
	public void signTrack(TransOrderScan transOrderScan)
	{
		TransOrderTrack transOrderTrack = new TransOrderTrack();
		transOrderTrack.setOrderNo(transOrderScan.getOrderNo());
		transOrderTrack.setTrackTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
		transOrderTrack.setTrackType("5");
		//根据签收状态判断签收情况
		if (TransConstants.SIGN_STATUS_SIGN.equals(transOrderScan.getSignStatus()))
		{
			transOrderTrack.setTrackInfo(String.format("收件人：%s已签收", transOrderScan.getSignName()));
		}else if (TransConstants.SIGN_STATUS_REJECTION.equals(transOrderScan.getSignStatus()))
		{
			transOrderTrack.setTrackInfo(String.format("收件人：%s已拒收，退回", transOrderScan.getSignName()));
		}
		save(transOrderTrack);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-7-20下午11:29:13
	 * @modified by
	 * @param orders
	 * @return
	 */
	@Override
	public List<TransOrderTrackItem> loadTracksByOrderNos(String orders) {
		List<TransOrderTrackItem> trackItems = new ArrayList<TransOrderTrackItem>();
		if (StringUtils.isNotBlank(orders)) {
			TransOrderTrackItem item = null;
			String[] orderArray = orders.split(",");
			for (String orderNo : orderArray) {
				TransOrder transOrder = transOrderService.getOrderByNo(orderNo);
				
				List<TransOrderTrack> tracks = transOrderTrackDao.findBy("orderNo", orderNo);
				for (TransOrderTrack transOrderTrack : tracks) {
					item = new TransOrderTrackItem();
					item.setOrderNo(orderNo);
					item.setStartWebsite(transOrder.getStartWebsite());
					item.setEndWebsite(transOrder.getEndWebsite());
					item.setGoodsName(transOrder.getGoodsName());
					item.setTrackInfo(transOrderTrack.getTrackInfo());
					item.setTrackTime(transOrderTrack.getTrackTime());
					trackItems.add(item);
				}
			}
		}
		return trackItems;
	}

}
